package com.yami.shop.common.minio;

import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
@Slf4j
public class MinioFileUtil {
    private static String url = "http://175.27.243.233:9000";

    private static String access = "file@admin";

    private static String secret = "file@admin";

    private static String bucket = "imgbucket";


    public static String minioUpload(MultipartFile file)  {
        try {
            MinioClient minioClient = MinioClient.builder().endpoint(url).credentials(access, secret).build();
            InputStream in = file.getInputStream();
            String fileName = file.getOriginalFilename();
            fileName = UUID.randomUUID() + "." + fileName;
            //创建头部信息
            Map headers = new HashMap<>(10);
            //添加自定义内容类型
            headers.put("Content-Type", file.getContentType());
            //添加存储类
            headers.put("X-Amz-Storage-Class", "REDUCED_REDUNDANCY");
            //添加自定义/用户元数据
            Map userMetadata = new HashMap<>(10);
            userMetadata.put("My-Project", "Project One");
            //上传
            minioClient.putObject(
                    PutObjectArgs.builder().bucket(bucket).object(fileName).stream(
                            in, in.available(), -1)
                            .headers(headers)
                            .userMetadata(userMetadata)
                            .build());
            in.close();
            return url+"/"+bucket+"/"+fileName;
        }catch (Exception e){
            e.printStackTrace();
            log.info("上传图片到minio异常，{}",e.getMessage());
        }
        return null;
    }



    public static void main(String[] args)  {
        try {

            //MinioClient minioClient = new MinioClient("http://localhost:9000", "JIA3V3GEZ606LM6PNEBX", "H8acR2RR3JLpLEb0ajfu/YIe8XgmcdkCsc+hk9hx");
            MinioClient minioClient = MinioClient.builder().endpoint(url).credentials(access, secret).build();
            File toFile = new File("e:/20220222155124.png");
            InputStream in = new FileInputStream(toFile);
            String fileName = toFile.getName();
            fileName = UUID.randomUUID() +"."+fileName;
            //创建头部信息
            Map headers = new HashMap<>(10);
            //添加自定义内容类型
            headers.put("Content-Type", "application/octet-stream");
            //添加存储类
            headers.put("X-Amz-Storage-Class", "REDUCED_REDUNDANCY");
            //添加自定义/用户元数据
            Map userMetadata = new HashMap<>(10);
            userMetadata.put("My-Project", "Project One");
            //上传
            minioClient.putObject(
                    PutObjectArgs.builder().bucket(bucket).object(fileName).stream(
                            in, in.available(), -1)
                            .headers(headers)
                            .userMetadata(userMetadata)
                            .build());
            in.close();
            System.out.println(url+"/"+bucket+"/"+fileName);
        } catch (Exception e) {

            System.out.println("Error occurred: " + e);

        }
    }
}
